package service

import (
	sqlc "bgs/db/sqlc"
	"bgs/util"
	"context"
	"encoding/json"
	"fmt"
	"time"
)

type loggingAccountForOrderBonusProfitExt struct {
	SpokesmanID               int64     `json:"spokesman_id"`
	SpokesmanUserID           int64     `json:"spokesman_user_id"`
	OrderSerialNumber         string    `json:"order_serial_number"`
	LedgerRequestSerialNumber string    `json:"ledger_request_serial_number"`
	Finished                  time.Time `json:"finished"`
	WxLedgerOrderNo           string    `json:"wx_ledger_order_no"`
	Scene                     string    `json:"scene"`
}

// LoggingAccountForOrderBonusProfitOfBrand 用户推广品牌订单奖励金记账
func (s *UserServiceImp) LoggingAccountForOrderBonusProfitOfBrand(ctx context.Context, q *sqlc.Queries, ledgerRequestPtr *sqlc.LedgerRequest, orderPtr *sqlc.Order, wxLedgerOrderNo string, occurred time.Time) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("==begin 用户推广品牌订单奖励金记账")

	orderSerialNumber := ledgerRequestPtr.OrderSerialNumber
	ledgerRequestSerialNumber := ledgerRequestPtr.SerialNumber
	brandID := orderPtr.BrandID
	spokesmanID := orderPtr.SpokesmanID.Int64
	spokesmanUserID := orderPtr.SpokesmanUserID.Int64
	cBrand := s.cacher.FetchBrand(ledgerRequestPtr.BrandID)
	slog.Infof("2、获取推广订单品牌缓存信息 ok")

	extInfoBytes, marshalErr := json.Marshal(&loggingAccountForOrderBonusProfitExt{
		SpokesmanID:               spokesmanID,
		SpokesmanUserID:           spokesmanUserID,
		OrderSerialNumber:         orderSerialNumber,
		LedgerRequestSerialNumber: ledgerRequestSerialNumber,
		Finished:                  occurred,
		WxLedgerOrderNo:           wxLedgerOrderNo,
		Scene:                     "品牌代言人奖励金",
	})
	if marshalErr != nil {
		err = marshalErr
		return
	}

	loggingParam := LoggingAccountParam{
		UserID:   spokesmanUserID,
		BrandID:  brandID,
		IsIn:     true,
		Category: sqlc.EmUserAccountLogCategoryOrderBonusProfit,
		Amount:   ledgerRequestPtr.BonusAmount,
		Subject:  fmt.Sprintf("奖励金(品牌)-%s", cBrand.Name),
		ExtInfo:  string(extInfoBytes),
	}

	err = s.loggingAccount(ctx, q, loggingParam)
	if err != nil {
		return
	}
	slog.Infof("3 用户账户变动日志(明细账)用户推广品牌订单奖励金 ok")

	slog.Infof("==end 用户推广品牌订单奖励金记账 ok")
	return
}

// LoggingAccountForOrderBonusProfitOfStore 用户推广门店订单奖励金记账
func (s *UserServiceImp) LoggingAccountForOrderBonusProfitOfStore(ctx context.Context, q *sqlc.Queries, ledgerRequestPtr *sqlc.LedgerRequest, orderPtr *sqlc.Order, wxLedgerOrderNo string, occurred time.Time) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("==begin 用户推广门店订单奖励金记账")

	orderSerialNumber := ledgerRequestPtr.OrderSerialNumber
	brandID := orderPtr.BrandID
	storeID := orderPtr.StoreID.Int64
	spokesmanID := orderPtr.SpokesmanID.Int64
	spokesmanUserID := orderPtr.SpokesmanUserID.Int64
	cStore := s.cacher.FetchStore(storeID)
	slog.Infof("2、获取推广订单门店缓存信息 ok")

	extInfoBytes, marshalErr := json.Marshal(&loggingAccountForOrderBonusProfitExt{
		SpokesmanID:       spokesmanID,
		SpokesmanUserID:   spokesmanUserID,
		OrderSerialNumber: orderSerialNumber,
		Finished:          occurred,
		WxLedgerOrderNo:   wxLedgerOrderNo,
		Scene:             "门店代言人奖励金",
	})
	if marshalErr != nil {
		err = marshalErr
		return
	}

	loggingParam := LoggingAccountParam{
		UserID:   spokesmanUserID,
		BrandID:  brandID,
		StoreID:  storeID,
		IsIn:     true,
		Category: sqlc.EmUserAccountLogCategoryOrderBonusProfit,
		Amount:   ledgerRequestPtr.BonusAmount,
		Subject:  fmt.Sprintf("奖励金(门店)-%s", cStore.Name),
		ExtInfo:  string(extInfoBytes),
	}

	err = s.loggingAccount(ctx, q, loggingParam)
	if err != nil {
		return
	}
	slog.Infof("3 用户账户变动日志(明细账)用户推广门店订单奖励金 ok")

	slog.Infof("==end 用户推广门店订单奖励金记账 ok")
	return
}

type loggingAccountForOrderBonusProfitRollbackExt struct {
	SpokesmanID                int64     `json:"spokesman_id"`
	SpokesmanUserID            int64     `json:"spokesman_user_id"`
	OrderSerialNumber          string    `json:"order_serial_number"`
	AfterSaleSerialNumber      string    `json:"after_sale_serial_number"`
	LedgerRollbackSerialNumber string    `json:"ledger_rollback_serial_number"`
	Finished                   time.Time `json:"finished"`
	WxLedgerReturnNo           string    `json:"wx_ledger_return_no"`
	Scene                      string    `json:"scene"`
}

// LoggingAccountForOrderBonusProfitRollbackOfBrand 用户推广品牌订单奖励金回退记账
func (s *UserServiceImp) LoggingAccountForOrderBonusProfitRollbackOfBrand(ctx context.Context, q *sqlc.Queries, ledgerRollbackPtr *sqlc.LedgerRollback, orderPtr *sqlc.Order, wxLedgerReturnNo string, occurred time.Time) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("==begin 用户推广品牌订单奖励金回退记账")

	brandID := orderPtr.BrandID
	spokesmanID := orderPtr.SpokesmanID.Int64
	spokesmanUserID := orderPtr.SpokesmanUserID.Int64
	cBrand := s.cacher.FetchBrand(ledgerRollbackPtr.BrandID)
	slog.Infof("1、获取推广订单品牌缓存信息 ok")

	orderSerialNumber := ledgerRollbackPtr.OrderSerialNumber
	afterSaleSerialNumber := ledgerRollbackPtr.AfterSaleSerialNumber
	ledgerRollbackSerialNumber := ledgerRollbackPtr.SerialNumber

	extInfoBytes, marshalErr := json.Marshal(&loggingAccountForOrderBonusProfitRollbackExt{
		SpokesmanID:                spokesmanID,
		SpokesmanUserID:            spokesmanUserID,
		OrderSerialNumber:          orderSerialNumber,
		AfterSaleSerialNumber:      afterSaleSerialNumber,
		LedgerRollbackSerialNumber: ledgerRollbackSerialNumber,
		Finished:                   occurred,
		WxLedgerReturnNo:           wxLedgerReturnNo,
		Scene:                      "代言人奖励金回退",
	})
	if marshalErr != nil {
		err = marshalErr
		return
	}

	loggingParam := LoggingAccountParam{
		UserID:   spokesmanUserID,
		BrandID:  brandID,
		IsIn:     false,
		Category: sqlc.EmUserAccountLogCategoryRollbackBonusProfit,
		Amount:   ledgerRollbackPtr.BonusAmount,
		Subject:  fmt.Sprintf("奖励金回退(品牌)-%s", cBrand.Name),
		ExtInfo:  string(extInfoBytes),
	}

	err = s.loggingAccount(ctx, q, loggingParam)
	if err != nil {
		return err
	}
	slog.Infof("3 用户账户变动日志(明细账)用户推广品牌订单奖励金回退 ok")

	slog.Infof("==end 用户推广品牌订单奖励金回退记账 ok")
	return
}

// LoggingAccountForOrderBonusProfitRollbackOfStore 用户推广门店订单奖励金回退记账
func (s *UserServiceImp) LoggingAccountForOrderBonusProfitRollbackOfStore(ctx context.Context, q *sqlc.Queries, ledgerRollbackPtr *sqlc.LedgerRollback, orderPtr *sqlc.Order, wxLedgerReturnNo string, occurred time.Time) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("==begin 用户推广门店订单奖励金回退记账")

	brandID := orderPtr.BrandID
	storeID := orderPtr.StoreID.Int64
	spokesmanID := orderPtr.SpokesmanID.Int64
	spokesmanUserID := orderPtr.SpokesmanUserID.Int64
	cStore := s.cacher.FetchStore(storeID)
	slog.Infof("1、获取推广订单门店缓存信息 ok")

	orderSerialNumber := ledgerRollbackPtr.OrderSerialNumber
	afterSaleSerialNumber := ledgerRollbackPtr.AfterSaleSerialNumber
	ledgerRollbackSerialNumber := ledgerRollbackPtr.SerialNumber

	extInfoBytes, marshalErr := json.Marshal(&loggingAccountForOrderBonusProfitRollbackExt{
		SpokesmanID:                spokesmanID,
		SpokesmanUserID:            spokesmanUserID,
		OrderSerialNumber:          orderSerialNumber,
		AfterSaleSerialNumber:      afterSaleSerialNumber,
		LedgerRollbackSerialNumber: ledgerRollbackSerialNumber,
		Finished:                   occurred,
		WxLedgerReturnNo:           wxLedgerReturnNo,
		Scene:                      "代言人奖励金回退",
	})
	if marshalErr != nil {
		err = marshalErr
		return
	}

	loggingParam := LoggingAccountParam{
		UserID:   spokesmanUserID,
		BrandID:  brandID,
		StoreID:  storeID,
		IsIn:     false,
		Category: sqlc.EmUserAccountLogCategoryRollbackBonusProfit,
		Amount:   ledgerRollbackPtr.BonusAmount,
		Subject:  fmt.Sprintf("奖励金回退(门店)-%s", cStore.Name),
		ExtInfo:  string(extInfoBytes),
	}

	err = s.loggingAccount(ctx, q, loggingParam)
	if err != nil {
		return err
	}
	slog.Infof("3 用户账户变动日志(明细账)用户推广门店订单奖励金回退 ok")

	slog.Infof("==end 用户推广门店订单奖励金回退记账 ok")
	return
}
